package com.hddz.acquisition.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hddz.acquisition.R;
import com.hddz.acquisition.activity.grzx.GRSZActivity;
import com.hddz.acquisition.activity.stock.ChangeStockActivity;
import com.hddz.acquisition.activity.stock.GroundingActivity;
import com.hddz.acquisition.activity.stock.MoreUnGroundingActivity;
import com.hddz.acquisition.activity.stock.OutStockActivity;
import com.hddz.acquisition.activity.stock.ProductOutStockListActivity;
import com.hddz.acquisition.activity.stock.PruductOutStockActivity;
import com.hddz.acquisition.activity.stock.ScanActivity;
import com.hddz.acquisition.activity.stock.ScanInvActivity;
import com.hddz.acquisition.activity.stock.ScanWarehouseActivity;
import com.hddz.acquisition.activity.stock.UnGroundingActivity;
import com.hddz.acquisition.view.MenuAdapter;
import com.hddz.acquisition.view.MenuOnPageChangeListener;
import com.hddz.acquisition.view.MenuPagerAdapter;

public class MainActivity20160507 extends Activity {

	private MenuPagerAdapter adapter;
	private ViewPager viewPager;
	private MenuOnPageChangeListener pageChangeListener;

	private int titles[] = new int[] { R.string.menu_spsj, R.string.menu_spxj,
			R.string.menu_dbck, R.string.menu_spyk, R.string.menu_plsm,
			R.string.menu_plxj, R.string.menu_kwsm, R.string.menu_spsm,
			R.string.menu_bpck, R.string.menu_bpqd, R.string.menu_grzx_grsz,
			R.string.menu_grzx_xtsz };
	private int icos[] = new int[] { R.drawable.menu_qaz_nfc_wh,
			R.drawable.menu_qaz_rfid_wh, R.drawable.menu_qaz_gps_dw,
			R.drawable.menu_qaz_ckzs, R.drawable.menu_qaz_gps_dw,
			R.drawable.menu_qaz_gps_dw, R.drawable.menu_qaz_gps_dw,
			R.drawable.menu_qaz_gps_dw, R.drawable.menu_qaz_gps_dw,
			R.drawable.menu_qaz_gps_dw, R.drawable.menu_grzx,
			R.drawable.menu_grzx_grsz };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);
		viewPager = (ViewPager) findViewById(R.id.viewpagerLayout);
		initViews();
		((TextView) findViewById(R.id.lbl_title)).setText(getResources()
				.getString(R.string.app_name));
	}

	OnItemClickListener listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			doAction(titles[pageChangeListener.getCurrIndex()
					* (int) MenuAdapter.APP_PAGE_SIZE + arg2]);
		}
	};

	public void onBackHome(View view) {
		finish();
	}

	public void initViews() {

		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);

		int pageSize = (int) Math.ceil(titles.length
				/ MenuAdapter.APP_PAGE_SIZE);
		ArrayList<GridView> array = new ArrayList<GridView>();
		for (int i = 0; i < pageSize; i++) {
			GridView appPage = new GridView(this);
			appPage.setAdapter(new MenuAdapter(this, titles, icos, i));
			appPage.setNumColumns(2);
			appPage.setOnItemClickListener(listener);
			array.add(appPage);
		}
		adapter = new MenuPagerAdapter(this, array);
		viewPager.setAdapter(adapter);

		ImageView[] points = new ImageView[pageSize];
		for (int i = 0; i < points.length; i++) {
			ImageView img = new ImageView(this);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			params.setMargins(5, 0, 5, 0);
			params.gravity = Gravity.CENTER_VERTICAL;
			img.setLayoutParams(params);
			img.setImageResource(R.drawable.selector_menu_ico);
			img.setEnabled(true);// 设置当前状态为允许（可点，灰色）
			img.setTag(i);// 标识符与圆点顺序一致
			points[i] = img;
			linearLayout.addView(img, i);
		}
		pageChangeListener = new MenuOnPageChangeListener(points);
		viewPager.setOnPageChangeListener(pageChangeListener);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		int index = -1;
		switch (keyCode) {
		case KeyEvent.KEYCODE_1:
			index = 0;
			break;
		case KeyEvent.KEYCODE_2:
			index = 1;
			break;
		case KeyEvent.KEYCODE_4:
			index = 2;
			break;
		case KeyEvent.KEYCODE_5:
			index = 3;
			break;
		}
		if (index >= 0) {
			doAction(titles[pageChangeListener.getCurrIndex()
					* (int) MenuAdapter.APP_PAGE_SIZE + index]);
		}
		return super.onKeyDown(keyCode, event);
	}

	private void doAction(int rid) {
		switch (rid) {
		case R.string.menu_spsj:
			startActivity(new Intent(MainActivity20160507.this,
					GroundingActivity.class));
			break;
		case R.string.menu_spxj:
			startActivity(new Intent(MainActivity20160507.this,
					UnGroundingActivity.class));
			break;
		case R.string.menu_dbck:
			startActivity(new Intent(MainActivity20160507.this,
					OutStockActivity.class));
			break;
		case R.string.menu_spyk:
			startActivity(new Intent(MainActivity20160507.this,
					ChangeStockActivity.class));
			break;
		case R.string.menu_plsm:
			startActivity(new Intent(MainActivity20160507.this,
					ScanActivity.class));
			break;
		case R.string.menu_grzx_grsz:
			startActivity(new Intent(MainActivity20160507.this,
					GRSZActivity.class));
			break;
		case R.string.menu_plxj:
			startActivity(new Intent(MainActivity20160507.this,
					MoreUnGroundingActivity.class));
			break;
		case R.string.menu_spsm:
			startActivity(new Intent(MainActivity20160507.this,
					ScanInvActivity.class));
			break;
		case R.string.menu_kwsm:
			startActivity(new Intent(MainActivity20160507.this,
					ScanWarehouseActivity.class));
			break;
		case R.string.menu_grzx_xtsz:
			startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
			break;
		case R.string.menu_bpck:
			startActivity(new Intent(MainActivity20160507.this,
					PruductOutStockActivity.class));
			break;
		case R.string.menu_bpqd:
			startActivity(new Intent(MainActivity20160507.this,
					ProductOutStockListActivity.class));
			break;
		}
	}
}
